import {
    mapGetters
} from "vuex";
import * as util from "@/common/util";
import i18n from "@/i18n";
import QRCode from 'qrcode'

export default {
    data() {
        return {
            // 国际化数据
            lang: i18n.t('m'),

            datePickerOptions: {
                // 快捷选项
                shortcuts: [{
                        text: i18n.t('m.Last_7days'),
                        onClick(picker) {
                            picker.$emit('pick', util.getAround7DayRangeTime())
                        }
                    },
                    {
                        text: i18n.t('m.month_'),
                        onClick(picker) {
                            picker.$emit('pick', util.getAroundOneMonthRangeTime())
                        }
                    },
                    {
                        text: i18n.t('m.Last6months'),
                        onClick(picker) {
                            picker.$emit('pick', util.getAroundHalfYearRangeTime())
                        }
                    },
                    {
                        text: i18n.t('m.Thisyear'),
                        onClick(picker) {
                            picker.$emit('pick', util.getAroundOneYearRangeTime())
                        }
                    }
                ],
            },
        }
    },
    computed: {
        ...mapGetters(['conf']),
        ...mapGetters(['activeUrl']),
        ...mapGetters(['parentId']),

        ...mapGetters(['userInfo']),
        ...mapGetters(['roleName']),
        ...mapGetters(['permissions']),
        ...mapGetters(['xt']),
        ...mapGetters(['currency']),

        // 文件主机
        fileHost() {
            return this.conf['file-host'] ? this.conf['file-host'] : ''
        },
        // 货币
        currencyVal() {
            let val = this.$currency.currency
            // console.log('货币', val)
            return val + ' '
        },
        // 货币符号
        symbolVal() {
            let val = this.$currency.symbol
            // console.log('货币符号', val)
            return val
        },

    },
    methods: {
        // 文件路径格式化
        fileUrlFormat(val) {
            return util.fileUrlFormat(val, this.fileHost)
        },
        // 检查金额
        checkAmount(val, min, max) {
            // console.log('checkAmount', val, min, max)
            return util.checkAmount(val, min, max)
        },
        // 检查数字
        checkNumber(val, min, max) {
            // console.log('checkNumber', val, min, max)
            return util.checkNumber(val, min, max)
        },
        // 打开链接
        openUrl(url, target) {
            // console.log('openUrl', url, target)
            window.open(url, target)
        },
        // 返回
        back() {
            this.$router.go(-1)
        },
        // 跳转路由页面
        jump(path, query) {
            // console.log('jump', path, query)
            if (path) {
                this.$router.push({
                    path: path,
                    query: query ? query : null
                })
                this.$store.commit('SET_ACTIVE_URL', path)
            }
        },

        // 渲染表头提示元素
        renderTips(h, title, content) {
            return h(
                'div', [
                    h('span', title),
                    h('el-tooltip', {
                        props: {
                            effect: 'dark',
                            placement: 'top',
                            content: content,
                        },
                    }, [
                        h('i', {
                            class: 'el-icon-question',
                            style: 'margin-left:3px;'
                        })
                    ])
                ]
            )
        },

        // 保存页码及搜索内容
        setPageNum(pageNums, pageSizes, searchParams) {
            this.$cache.set('pageNums', pageNums)
            this.$cache.set('pageSizes', pageSizes)
            this.$cache.set('searchParams', searchParams)
        },

        // 移除页数码
        remPageNum() {
            this.$cache.remove('pageNums')
            this.$cache.remove('pageSizes')
            this.$cache.remove('searchParams')
        },

        //权限id校验
        permissionsId(val) {
            let map = []
            this.$store.state.permissions.forEach(item => {
                map.push(Number(this.$jsencrypt.decrypt(item)))
            })
            return map.includes(val)
        },

        //校验是否存在权限组
        isArrayEqual(arr, method) {
            if (arr.some(item => method(item))) {
                return true
            } else {
                return false
            }
        },

        //获取用户类型
        getUserType() {
            let userType = this.$jsencrypt.decrypt(this.$cache.get('userType'))
            return userType
        },

        //获取代理商等级
        getAgentLevel() {
            let agentLevel = this.$jsencrypt.decrypt(this.$cache.get('agentLevel'))
            return agentLevel
        },


        //转成二维码
        scanaQRcode(QRcode) {
            let canvas = document.createElement('canvas')
            let url = ''
            QRCode.toCanvas(canvas, QRcode, error => {
                if (error) {
                    this.$message.error(error)
                }
                url = canvas.toDataURL('image/jpeg')
            })
            return url
        },


        //获取代理商等级
        getRoleName() {
            let roleName = this.$jsencrypt.decrypt(this.$cache.get('roleName'))
            return roleName
        },

    }
}